import time
import alg
import generator

def test_algorithm(algorithm, data):
    start_time = time.time()
    sorted_data = algorithm(data.copy())  #复制数据，避免修改原始数据
    end_time = time.time()
    run_time = end_time - start_time

    # 验证排序是否正确
    expected_result = sorted(data)
    is_correct = (sorted_data == expected_result)

    print(f"{algorithm.__name__}排序后的数据:", sorted_data)
    print(f"{algorithm.__name__}排序是否正确排序:", is_correct)
    print(f"{algorithm.__name__}排序运行时间:", run_time, "秒")

    #return is_correct, run_time, sorted_data


if __name__ == '__main__':
    # 1. 生成测试数据
    data_size = 20
    test_data = generator.generate_data(data_size)  # 使用简化后的generate_data

    print("初始随机数据：",test_data)

    test_algorithm(alg.bubble_sort,test_data)

    test_algorithm(alg.quick_sort, test_data)

    test_algorithm(alg.shell_sort, test_data)

    test_algorithm(alg.insertion_sort, test_data)


"""   # 2. 调用测试函数
    is_correct, run_time, sorted_data = test_algorithm(alg.bubble_sort, test_data)

    # 3. 输出测试结果
    print("原始数据:", test_data)
    
    print("冒泡排序排序后的数据:", sorted_data)
    print("冒泡排序是否正确排序:", is_correct)
    print("冒泡排序运行时间:", run_time, "秒")
"""